package com.sgb.kjwl.viewmodel.manage;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.manage.EpAssestEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.ManageRepertory;
import com.sgb.kjwl.view.ui.fragment.manage.EpAssestFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EpAssestFragModel extends BaseViewModel {
    private EpAssestFragment fragment;
    private ManageRepertory repertory;
    private int pageNum = 1;

    public EpAssestFragModel(EpAssestFragment fragment, ManageRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }


    public void getAssestList(boolean isLodMore, Map map) {
        pageNum = isLodMore ? pageNum + 1 : 1;
        //limit
        map.put("limit", 20);
        //page
        map.put("page", pageNum);
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getAssestList(map).enqueue(new Callback<BaseEntity<ListResponse<List<EpAssestEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<EpAssestEntity>>>> call, Response<BaseEntity<ListResponse<List<EpAssestEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showList(response.body().getData().getList(), isLodMore);
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<EpAssestEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
